var messageId = ""


layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;


    //获取当前用户id和部门
    var userId = '';
    var department = '';
    $.ajax(Feng.ctxPath + "/databaseListKh/getUser", {
        async: false,
        success: function (res) {
            // console.log(res.data)
            userId = res.data.id
            department = res.data.deptName
        }
    })

    $(function () {
        insertBtn();
    })

    //申请按钮和第二次点进来按钮禁用
    function insertBtn() {
        var downloadDone = sessionStorage.getItem("downloadDone")
        sessionStorage.clear();
        var a = '<div class="layui-col-md2 layui-col-md-offset1">';
        a += '<button class="layui-btn layui-btn-normal" id="apply">&nbsp;申请&nbsp;</button>';
        a += '</div>';

        var b = '<div class="layui-col-md2 layui-col-md-offset1">';
        b += '<button class="layui-btn layui-btn-normal layui-btn-disabled" disabled>&nbsp;已申请&nbsp;</button>';
        b += '</div>';
        $("#btn").empty();

        if (downloadDone == 1) {
            $.ajax(Feng.ctxPath + '/interfaceapproval/list', {
                data: {
                    userId: userId,
                    echo: 2
                },
                success: function (res) {
                    res.data[0].echo = 0;
                    $.ajax(Feng.ctxPath + '/interfaceapproval/editItem', {
                        data: res.data[0]
                    })
                }
            })
        } else {
            $.ajax(Feng.ctxPath + '/interfaceapproval/list', {
                data: {
                    userId: userId,
                    echo: 2
                },
                success: function (res) {
                    console.log(res)
                    var c = ''
                    if (res.data.length != 0) {
                        messageId = res.data[0].id
                        console.log(messageId)
                        if (res.data[0].state == 1) {
                            c += '<div class="layui-col-md12" style="margin-top: 35px">';
                            c += '<span class="layui-col-md1" style="text-align: right">状态：</span>';
                            c += '<span class="layui-col-md5" style="margin-left: 20px">' + '已审核' + '</span>';
                            c += '</div>';
                            c += '<div class="layui-col-md12" style="margin-top: 35px">';
                            c += '<span class="layui-col-md1" style="text-align: right">结果反馈：</span>';
                            c += '<span class="layui-col-md5" style="margin-left: 20px">' + '审核通过,等待反馈......' + '</span>';
                            c += '</div>';
                        } else if (res.data[0].state == 0) {
                            c += '<div class="layui-col-md12" style="margin-top: 35px">';
                            c += '<span class="layui-col-md1" style="text-align: right">状态：</span>';
                            c += '<span class="layui-col-md5" style="margin-left: 20px">' + '已申请' + '</span>';
                            c += '</div>';
                            c += '<div class="layui-col-md12" style="margin-top: 35px">';
                            c += '<span class="layui-col-md1" style="text-align: right">结果反馈：</span>';
                            c += '<span class="layui-col-md5" style="margin-left: 20px">' + '等待审批中......' + '</span>';
                            c += '</div>';
                        } else if (res.data[0].state == 3) {
                            $.ajax(Feng.ctxPath + '/data/selectByList', {
                                data: {
                                    messageId: messageId
                                },
                                async: false,
                                success: function (obj) {
                                    console.log(obj)
                                    c += '<div class="layui-col-md12" style="margin-top: 35px">';
                                    c += '<span class="layui-col-md1" style="text-align: right">状态：</span>';
                                    c += '<span class="layui-col-md5" style="margin-left: 20px">' + '已完成' + '</span>';
                                    c += '</div>';
                                    c += '<div class="layui-col-md12" style="margin-top: 35px">';
                                    c += '<span class="layui-col-md1" style="text-align: right">结果反馈：</span>';
                                    c += '<span class="layui-col-md5" style="margin-left: 20px">' + obj.data[0].remark + '</span>';
                                    c += '</div>';
                                    c += '<div class="layui-col-md12" style="margin-top: 30px">';
                                    c += '<span class="layui-col-md1" style="text-align: right">&nbsp;</span>';
                                    c += '<button id="fileDownload" class="layui-btn icon-btn" type="button"><i class="layui-icon">&#xe67d;</i>下载文件</button>';
                                    c += '</div>';

                                }
                            })
                        }
                        $('#description').val(res.data[0].description)
                        $('#description').attr('disabled', true)
                        $('#system').val(res.data[0].system)
                        $('#system').attr('disabled', true)
                        $('#display').append(c)
                        $('#btn').append(b)
                    } else {
                        $('#description').attr('disabled', false)
                        $('#system').attr('disabled', false)
                        $('#btn').append(a)
                    }
                }
            })
        }
    }


    //点击申请
    $('#btn').on('click', '#apply', function () {
        if ($('#description').val() == "" || $('#system').val() == "") {
            Feng.error("请填写必填项")
            return
        }
        $.ajax(Feng.ctxPath + '/interfaceapproval/addItem', {
            data: {
                userId: userId,
                content: 1,
                department: department,
                echo: 2,
                description: $('#description').val(),
                system: $('#system').val(),
                servicetype: 1,
                state: 0,
            },
            async: false,
            success: function (data) {
                console.log(data)
                // $("#echo").attr("value",'echo')
                history.go(-1)
            }
        })
    })

    /**
     * 点击下载文件
     */
    $("#display").on('click', '#fileDownload', function () {
        sessionStorage.setItem("downloadDone", 1);
        window.location = Feng.ctxPath + '/data/downloadFile?messageId=' + messageId
    })

    //返回按钮
    $("#goBack").click(function () {
        history.go(-1)
    })

});
